package cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.service;

import cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.mapper.RoleMapper;
import cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.pojo.Role;
import cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class RoleServiceImpI implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        // 1.开启分页功能
        PageHelper.startPage(pageNum, pageSize);

        // 2.执行查询
        RoleExample roleExample=new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Role> list = roleMapper.selectByExample(roleExample);
        // 3.封装为PageInfo对象
        return new PageInfo<>(list);
    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        // 创建实体类Role对应的Example对象
        RoleExample roleExample = new RoleExample();
        // 在Example对象中封装查询条件
        roleExample.createCriteria().andIdIn(roleIdList);
        // 执行查询
        return roleMapper.selectByExample(roleExample);
    }
    @Override
    public void batchRemove(List<Integer> roleIdList) {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(roleIdList);
        roleMapper.deleteByExample(roleExample);
    }
    @Override
    public void saveRole(String roleName) {
        roleMapper.insert(new Role(null, roleName));
    }
    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return roleMapper.getAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.getUnAssignedRoleList(adminId);
    }

    @Override
    public void deleteAdminRelationRole(Integer adminId) {
        roleMapper.deletAdminRelationRole(adminId);
    }

    @Override
    public void insertAdminRelationRole(Integer adminId, List<Integer> roleIdList) {
        roleMapper.insertAdminRelationRole(adminId, roleIdList);
    }

    @Override
    public void updateRelationship(Integer adminId, List<Integer> roleIdList) {
        //删除旧的
        roleMapper.deletAdminRelationRole(adminId);
        //添加新的
        roleMapper.insertAdminRelationRole(adminId,roleIdList);
    }
}
